package com.qj.daibei.controller;

import com.qj.daibei.pojo.DataFiling;
import com.qj.daibei.pojo.User;
import com.qj.daibei.service.DataFilingService;
import com.qj.daibei.service.UserService;
import com.qj.daibei.util.FileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

/**
 * author wml
 * Created on 2021/9/23 11:40
 */
@Controller
public class DataFilingController {

    @Autowired
    private DataFilingService dataFilingService;

    @Autowired
    private UserService userService;

    @RequestMapping("/updateDataFiling")
    public ResponseEntity<String> updateDataFiling(DataFiling dataFiling, @RequestParam("file") MultipartFile[] multipartFile, HttpServletRequest request) throws IOException {
        HttpSession session = request.getSession(false);
        if (session == null) {
            return new ResponseEntity<String>("noSession", HttpStatus.OK);
        }
        User user = (User) session.getAttribute("loginUser");
        dataFiling.setUserId(user.getUserId());

        String[] fileNames = new String[5];
        for (int i = 0; i < multipartFile.length; i++) {
            fileNames[i] = FileUpload.doSingleFileUpload(multipartFile[i], "dataFiling");
            System.out.println(fileNames[i]);
        }
        if (!fileNames[0].substring(fileNames[0].lastIndexOf(".")).equals(".")) {
            dataFiling.setIdfv(fileNames[0]);
        }
        if (!fileNames[1].substring(fileNames[1].lastIndexOf(".")).equals(".")) {
            dataFiling.setIdrv(fileNames[1]);
        }
        if (!fileNames[2].substring(fileNames[2].lastIndexOf(".")).equals(".")) {
            dataFiling.setPropertyOwnership(fileNames[2]);
        }
        if (!fileNames[3].substring(fileNames[3].lastIndexOf(".")).equals(".")) {
            dataFiling.setVehicleLicense(fileNames[3]);
        }
        if (!fileNames[4].substring(fileNames[4].lastIndexOf(".")).equals(".")) {
            dataFiling.setBankStatement(fileNames[4]);
        }

        dataFiling.setAssessmentTypeA(1);
        dataFiling.setAssessmentTypeB(1);
        dataFiling.setFillIn("1");
        dataFiling.setCreateTime(new Date());
        dataFiling.setCommentA("");
        dataFiling.setCommentB("");

//        System.out.println(dataFiling.toString());
        int result = dataFilingService.updateDataFiling(dataFiling);
        return new ResponseEntity<String>("ok", HttpStatus.OK);
    }

    @RequestMapping("/queryForAssessmentType")
    public ResponseEntity<String> queryForAssessmentType(HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        DataFiling dataFiling = this.dataFilingService.queryForRecordByUserId(user.getUserId());
        if (dataFiling.getAssessmentTypeA() == 1) {
            return new ResponseEntity<String>("", HttpStatus.OK);
        } else if (dataFiling.getAssessmentTypeA() == 2 || (dataFiling.getAssessmentTypeA() == 4 && (dataFiling.getAssessmentTypeB() == 1 || dataFiling.getAssessmentTypeB() == 2))) {
            return new ResponseEntity<String>("正在审核中，", HttpStatus.OK);
        } else {
            return new ResponseEntity<String>("jump", HttpStatus.OK);
        }
    }

    @RequestMapping("/queryForAssessmentType2")
    public ResponseEntity<String> queryForAssessmentType2(HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        DataFiling dataFiling = this.dataFilingService.queryForRecordByUserId(user.getUserId());
        if (dataFiling.getAssessmentTypeA() == 3 || dataFiling.getAssessmentTypeB() == 3) {
            if (dataFiling.getCommentA() != null && dataFiling.getCommentA() != "") {
                return new ResponseEntity<String>(dataFiling.getCommentA(), HttpStatus.OK);
            } else if (dataFiling.getCommentB() != null && dataFiling.getCommentB() != "") {
                return new ResponseEntity<String>(dataFiling.getCommentB(), HttpStatus.OK);
            } else {
                return new ResponseEntity<String>("未通过", HttpStatus.OK);
            }
        } else if (dataFiling.getAssessmentTypeA() == 4 && dataFiling.getAssessmentTypeB() == 4) {
            return new ResponseEntity<String>("已通过", HttpStatus.OK);
        } else {
            return new ResponseEntity<String>("未知的错误", HttpStatus.OK);
        }
    }

    @RequestMapping("/queryForMoney")
    public ResponseEntity<String> queryForMoney(HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        DataFiling dataFiling = this.dataFilingService.queryForRecordByUserId(user.getUserId());
        if (dataFiling.getResult() == null || dataFiling.getResult() == 0) {
            return new ResponseEntity<String>("未通过", HttpStatus.OK);
        } else if (dataFiling.getResult() != null) {
            return new ResponseEntity<String>(dataFiling.getResult().toString(), HttpStatus.OK);
        } else {
            return new ResponseEntity<String>("未知的错误", HttpStatus.OK);
        }
    }

    @RequestMapping("/queryDataFiling")
    @ResponseBody
    public DataFiling queryDataFiling(HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        DataFiling dataFiling = this.dataFilingService.queryForRecordByUserId(user.getUserId());
        return dataFiling;
    }

    @RequestMapping("/updateDataFilingAndUser")
    public String updateDataFilingAndUser(HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        int dataFilingResult = this.dataFilingService.updateDataFilingFillInByUserId(user.getUserId());
        int userResult = this.userService.updateUserFillInByUserId(user.getUserId());
        return "redirect:borrowing";
    }
}
